package org.chen.org.controller;


import io.swagger.annotations.Api;
import org.chen.bas.domain.BasDomain;
import org.chen.bas.util.AjaxResult;
import org.chen.bas.util.PageInfo;
import org.chen.org.query.DepartmentQuery;
import org.chen.org.service.IDepartmentService;
import org.chen.org.doamin.Department;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;
@Api(value = "部门管理",description = "部门操作的CRUD")
@RequestMapping("/department")
@RestController
public class DepartmentController {
    @Autowired
    private IDepartmentService service;

    //    @ApiOperation(value = "更新或新增")
    @PutMapping
    public AjaxResult save(@RequestBody Department department) {

        try {
            if (Objects.isNull(department.getId())) {
                service.insert(department);
            } else {
                service.update(department);
            }
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }

    }

    // 删 delete
    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id") Long id) {
        // 有返回值
        try {
            service.delete(id);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }

    // 查全部 get
    @GetMapping
    public AjaxResult selectList() {
        // 有返回值
        try {
            List<Department> departments = service.selectAll();
            return AjaxResult.success(departments);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }


    }

    // 查一个 get
    @GetMapping("/{id}")
    public AjaxResult selectByID(@PathVariable("id") Long id) {
        try {
             Department department = service.selectById(id);
            return AjaxResult.success(department);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }
    //分页查询
    @PostMapping()
    public AjaxResult query(@RequestBody DepartmentQuery departmentQuery) {
        try {
           PageInfo pageInfo =service.queryPage(departmentQuery);
            return AjaxResult.success(pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }
    @GetMapping("/super")
    public AjaxResult getSuper() {
        try {
            List<Department> department = service.getSuper();
            return AjaxResult.success(department);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }
}
